<?php

/**
 * profile form base class.
 *
 * @method profile getObject() Returns the current form's model object
 *
 * @package    sfwixet
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseprofileForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'user_id'         => new sfWidgetFormInputHidden(),
      'visits'          => new sfWidgetFormInputText(),
      'gender'          => new sfWidgetFormInputText(),
      'uid'             => new sfWidgetFormInputText(),
      'birthday'        => new sfWidgetFormDate(),
      'city_id'         => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('city'), 'add_empty' => true)),
      'country_id'      => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('country'), 'add_empty' => true)),
      'town_id'         => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('town'), 'add_empty' => true)),
      'created_at'      => new sfWidgetFormDateTime(),
      'updated_at'      => new sfWidgetFormDateTime(),
      'places_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'place')),
      'workplaces_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'workplace')),
      'schools_list'    => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'school')),
      'cityplace_list'  => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'city')),
      'groups_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'aclGroup')),
    ));

    $this->setValidators(array(
      'user_id'         => new sfValidatorChoice(array('choices' => array($this->getObject()->get('user_id')), 'empty_value' => $this->getObject()->get('user_id'), 'required' => false)),
      'visits'          => new sfValidatorInteger(array('required' => false)),
      'gender'          => new sfValidatorInteger(array('required' => false)),
      'uid'             => new sfValidatorString(array('max_length' => 25, 'required' => false)),
      'birthday'        => new sfValidatorDate(array('required' => false)),
      'city_id'         => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('city'), 'required' => false)),
      'country_id'      => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('country'), 'required' => false)),
      'town_id'         => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('town'), 'required' => false)),
      'created_at'      => new sfValidatorDateTime(),
      'updated_at'      => new sfValidatorDateTime(),
      'places_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'place', 'required' => false)),
      'workplaces_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'workplace', 'required' => false)),
      'schools_list'    => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'school', 'required' => false)),
      'cityplace_list'  => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'city', 'required' => false)),
      'groups_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'aclGroup', 'required' => false)),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorDoctrineUnique(array('model' => 'profile', 'column' => array('uid')))
    );

    $this->widgetSchema->setNameFormat('profile[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'profile';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['places_list']))
    {
      $this->setDefault('places_list', $this->object->places->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['workplaces_list']))
    {
      $this->setDefault('workplaces_list', $this->object->workplaces->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['schools_list']))
    {
      $this->setDefault('schools_list', $this->object->schools->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['cityplace_list']))
    {
      $this->setDefault('cityplace_list', $this->object->cityplace->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['groups_list']))
    {
      $this->setDefault('groups_list', $this->object->Groups->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveplacesList($con);
    $this->saveworkplacesList($con);
    $this->saveschoolsList($con);
    $this->savecityplaceList($con);
    $this->saveGroupsList($con);

    parent::doSave($con);
  }

  public function saveplacesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['places_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->places->getPrimaryKeys();
    $values = $this->getValue('places_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('places', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('places', array_values($link));
    }
  }

  public function saveworkplacesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['workplaces_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->workplaces->getPrimaryKeys();
    $values = $this->getValue('workplaces_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('workplaces', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('workplaces', array_values($link));
    }
  }

  public function saveschoolsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['schools_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->schools->getPrimaryKeys();
    $values = $this->getValue('schools_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('schools', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('schools', array_values($link));
    }
  }

  public function savecityplaceList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['cityplace_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->cityplace->getPrimaryKeys();
    $values = $this->getValue('cityplace_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('cityplace', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('cityplace', array_values($link));
    }
  }

  public function saveGroupsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['groups_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Groups->getPrimaryKeys();
    $values = $this->getValue('groups_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Groups', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Groups', array_values($link));
    }
  }

}
